#include "drawMenu.h"

drawMenu::drawMenu(drawArea *parent): QMenu(parent), area(parent){
	
	open  = new QAction(tr("Otevřít"), this);
	open->setIcon(QIcon(":img/document-open.png"));
	open->setShortcut(tr("Ctrl+O"));
	open->setStatusTip(tr("Otevře existující soubor."));
	connect(open, SIGNAL(triggered()), getTopWidget(), SLOT(openSlot()));
	
	save = new QAction(tr("Ulozit"), this);
	save->setIcon(QIcon(":img/document-save.png"));
	save->setShortcut(tr("Ctrl+S"));
	save->setStatusTip(tr("Uloží dokument na disk."));
	connect(save, SIGNAL(triggered()), getTopWidget(), SLOT(saveSlot()));
	
	saveAs = new QAction(tr("Ulozit jako"), this);
	saveAs->setIcon(QIcon(":img/document-save-as.png"));
	saveAs->setStatusTip(tr("Uloží dokument na disk pod novým jménem."));
	connect(saveAs, SIGNAL(triggered()), getTopWidget(), SLOT(saveAsSlot()));
	
	focus = new QAction(tr("Přizpůsobit"), this);
	focus->setStatusTip(tr("Přizpůsobí daný blok velikosti okna."));
	connect(focus, SIGNAL(triggered()), area, SLOT(focus()));
	
	numerate = new QAction(tr("Očíslovat"), this);
	numerate->setCheckable(true);
	numerate->setStatusTip(tr("Očísluje návaznost bloků."));
	connect(numerate, SIGNAL(triggered()), area, SLOT(setNumerate()));
	
	showMiddle = new QAction(tr("Střed kružnice."), this);
	showMiddle->setStatusTip(tr("Zobrazí střed kružnice."));
	connect(showMiddle, SIGNAL(triggered()), area, SLOT(middle()));
	
	distance = new QAction(tr("Vzdálenost"), this);
	distance->setCheckable(true);
	distance->setStatusTip(tr("Měření vzdálenosti mezi dvěma body."));
	connect(distance, SIGNAL(triggered()), area, SLOT(setDistanceMode()));
	
	viewMenu = new QMenu(tr("Měřítko"));
	
	zoomIn = new QAction(tr("Přiblížit"), this);
	zoomIn->setIcon(QIcon(":img/zoom-in.png"));
	zoomIn->setStatusTip(tr("Přiblížení pohledu."));
	connect(zoomIn, SIGNAL(triggered()), this, SLOT(zoomInSlot()));
	
	zoomOut = new QAction(tr("Oddálit"), this);
	zoomOut->setIcon(QIcon(":img/zoom-out.png"));
	zoomOut->setStatusTip(tr("Oddálit pohled."));
	connect(zoomOut, SIGNAL(triggered()), this, SLOT(zoomOutSlot()));
	
	zoomOriginal = new QAction(tr("Originál"), this);
	zoomOriginal->setIcon(QIcon(":img/zoom-fit-best.png"));
	zoomOriginal->setStatusTip(tr("Použít měřítko 1:1."));
	connect(zoomOriginal, SIGNAL(triggered()), this, SLOT(zoomOriginalSlot()));
	
	setMenu();

}

void drawMenu::setMenu(){

	gTypeGo *go;
	const controlPoints &control = area->getControl();

	if(!control.isActive()){
		addAction(open);
		addAction(save);
		addAction(saveAs);
		addSeparator();
	}
	else{
		go = control.getActiveGTypeGo();
		if(
			go->getTypeValue() == TYPE_ARC_CLOCK || 
			go->getTypeValue() == TYPE_ARC_COUNTER
			)
			addAction(showMiddle);
	}
	addAction(focus);
	addAction(numerate);
	if(control.isClicked())
		addAction(distance);
	addMenu(viewMenu);
	viewMenu->addAction(zoomIn);
	viewMenu->addAction(zoomOut);
	viewMenu->addAction(zoomOriginal);
		
}

void drawMenu::focusSlot(){

	area->focus();

}

void drawMenu::setDistance(bool boolean){
	
	distance->setChecked(boolean);
	
}

void drawMenu::setNumerate(bool boolean){

	numerate->setChecked(boolean);
	update();

}

const QWidget *drawMenu::getTopWidget() const{
	
	const QWidget *parent = this;
	
	while(parent->parentWidget()){
		parent = parent->parentWidget();
	}
	return parent;

}

void drawMenu::zoomInSlot(){
	
	QPointF point(1.1, 1.1);

	area->setScale(point);

}

void drawMenu::zoomOutSlot(){

	QPointF point(0.9, 0.9);

	area->setScale(point);

}

void drawMenu::zoomOriginalSlot(){

	QPointF point;

	area->getScale(point);
	point.setX(1 / point.x());
	point.setY(1 / point.y());
	area->setScale(point);

}
